package com.shop.common.mapper.setting;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.yulichang.query.MPJQueryWrapper;
import com.shop.common.config.GlobalConfig;
import com.shop.common.core.basics.IBaseMapper;
import com.shop.common.entity.setting.DictData;
import org.apache.ibatis.annotations.Mapper;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 字典数据Mapper
 */
@Mapper
public interface DictDataMapper extends IBaseMapper<DictData> {

    /**
     * 根据类型ID获取全部字典值
     */
    default List<DictData> selectByTypeId(Integer typeId){
        return this.selectList(new QueryWrapper<DictData>().eq("type_id", typeId));
    }

    /**
     * 根据字典类型获取全部字典值
     */
    default List<DictData> selectByType(String type) {
        MPJQueryWrapper<DictData> queryWrapper = new MPJQueryWrapper<>();
        queryWrapper.leftJoin("?_dict_type type ON t.type_id = type.id".replace("?_", GlobalConfig.tablePrefix))
                .eq("type.dict_type", type);
        queryWrapper.selectAll(DictData.class);
        return this.selectList(queryWrapper);
    };

    /**
     * 根据类型ID获取全部字典值Map
     */
    default Map<String, DictData> selectMapByTypeId(Integer typeId) {
        List<DictData> list = selectByTypeId(typeId);
        // 字典转成map
        Map<String, DictData> dictMap = new HashMap<>();
        for (DictData dictData : list) {
            dictMap.put(dictData.getValue(), dictData);
        }
        return dictMap;
    }

    /**
     * 根据字典类型获取全部字典值Map
     */
    default Map<String, DictData> selectMapByType(String type) {
        List<DictData> list = selectByType(type);
        // 字典转成map
        Map<String, DictData> dictMap = new HashMap<>();
        for (DictData dictData : list) {
            dictMap.put(dictData.getValue(), dictData);
        }
        return dictMap;
    }

}
